<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>[Modern C++]现代C++中优雅的异步操作 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="背景现代C++提供了一些很好的特性可以让我们实现优雅、高效的异步操作，今天向大家简要地介绍一下。
std::async1std::async(std::launch::async | std::launch::deferred, f, args...)

f为需要异步执行的函数，
args为f函数的参数，
第一个参数可选，在Visiual C++中默认为std::launch::async,即创建">
<meta property="og:type" content="article">
<meta property="og:title" content="[Modern C++]现代C++中优雅的异步操作">
<meta property="og:url" content="http://yoursite.com/2017/03/31/Modern-C-现代C-中优雅的异步操作/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="背景现代C++提供了一些很好的特性可以让我们实现优雅、高效的异步操作，今天向大家简要地介绍一下。
std::async1std::async(std::launch::async | std::launch::deferred, f, args...)

f为需要异步执行的函数，
args为f函数的参数，
第一个参数可选，在Visiual C++中默认为std::launch::async,即创建">
<meta property="og:updated_time" content="2017-03-31T14:11:20.317Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="[Modern C++]现代C++中优雅的异步操作">
<meta name="twitter:description" content="背景现代C++提供了一些很好的特性可以让我们实现优雅、高效的异步操作，今天向大家简要地介绍一下。
std::async1std::async(std::launch::async | std::launch::deferred, f, args...)

f为需要异步执行的函数，
args为f函数的参数，
第一个参数可选，在Visiual C++中默认为std::launch::async,即创建">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Modern-C-现代C-中优雅的异步操作" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2017/03/31/Modern-C-现代C-中优雅的异步操作/" class="article-date">
  <time datetime="2017-03-31T14:10:40.000Z" itemprop="datePublished">2017-03-31</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      [Modern C++]现代C++中优雅的异步操作
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h1><p>现代C++提供了一些很好的特性可以让我们实现优雅、高效的异步操作，今天向大家简要地介绍一下。</p>
<h1 id="std-async"><a href="#std-async" class="headerlink" title="std::async"></a>std::async</h1><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">std</span>::async(<span class="built_in">std</span>::launch::async | <span class="built_in">std</span>::launch::deferred, f, args...)</div></pre></td></tr></table></figure>
<ul>
<li>f为需要异步执行的函数，</li>
<li>args为f函数的参数，</li>
<li>第一个参数可选，在Visiual C++中默认为std::launch::async,即创建的future对象在创建完成后立即异步执行，当第一个参数为std::launch::deferred时表示future对象延迟执行。<h1 id="样例"><a href="#样例" class="headerlink" title="样例"></a>样例</h1><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;future&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;thread&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;chrono&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</div><div class="line">    <span class="keyword">auto</span> future1 = async([](<span class="keyword">int</span> &amp;&amp;x)-&gt;<span class="keyword">int</span>&#123;</div><div class="line">        <span class="keyword">auto</span> ret = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> i = <span class="number">0</span>; i &lt; x; i++)&#123;</div><div class="line">            this_thread::sleep_for(chrono::seconds(<span class="number">1</span>));</div><div class="line">            ret += i;</div><div class="line">            <span class="built_in">cout</span> &lt;&lt; <span class="string">"Task 1 calculating to "</span>  &lt;&lt; x - <span class="number">1</span> &lt;&lt; <span class="string">" and now the turn is "</span> &lt;&lt; i &lt;&lt; <span class="string">" and the value is "</span> &lt;&lt; ret &lt;&lt; <span class="built_in">endl</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ret;</div><div class="line">    &#125;,<span class="number">10</span>);    <span class="comment">//future1 立即异步执行</span></div><div class="line">    <span class="keyword">auto</span> future2 = async([](<span class="keyword">int</span> &amp;&amp;x)-&gt;<span class="keyword">int</span>&#123;</div><div class="line">        <span class="keyword">auto</span> ret = <span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> i = <span class="number">1</span>; i &lt; x + <span class="number">1</span>; i++)&#123;</div><div class="line">            this_thread::sleep_for(chrono::seconds(<span class="number">2</span>));</div><div class="line">            ret *= i;</div><div class="line">            <span class="built_in">cout</span> &lt;&lt; <span class="string">"Task 2 calculating to "</span>  &lt;&lt; x &lt;&lt; <span class="string">" and now the turn is "</span> &lt;&lt; i &lt;&lt; <span class="string">" and the value is "</span> &lt;&lt; ret &lt;&lt; <span class="built_in">endl</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ret;</div><div class="line">    &#125;,<span class="number">5</span>);     <span class="comment">//future2 立即异步执行</span></div><div class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">"waiting...\n"</span>;</div><div class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Result of task 1: "</span> &lt;&lt; future1.get() &lt;&lt; <span class="built_in">endl</span>;</div><div class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Result of task 2: "</span> &lt;&lt; future2.get() &lt;&lt; <span class="built_in">endl</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">c:\Users\yixian\workspace\test&gt;async</div><div class="line">waiting...</div><div class="line">Task 1 calculating to 9 and now the turn is 0 and the value is 0</div><div class="line">Task 1 calculating to 9 and now the turn is 1 and the value is 1</div><div class="line">Task 2 calculating to 5 and now the turn is 1 and the value is 1</div><div class="line">Task 2 calculating to 5 and now the turn is 2 and the value is 2</div><div class="line">Task 2 calculating to 5 and now the turn is 3 and the value is 6Task 1 calculating to 9 and now the turn is 2 and the value is 3</div><div class="line">Task 1 calculating to 9 and now the turn is 3 and the value is 6</div><div class="line">Task 1 calculating to 9 and now the turn is 4 and the value is 10</div><div class="line">Task 1 calculating to 9 and now the turn is 5 and the value is 15</div><div class="line">Task 1 calculating to 9 and now the turn is 6 and the value is 21</div><div class="line">Task 1 calculating to 9 and now the turn is 7 and the value is 28</div><div class="line">Task 1 calculating to 9 and now the turn is 8 and the value is 36</div><div class="line">Task 1 calculating to 9 and now the turn is 9 and the value is 45</div><div class="line"></div><div class="line">Task 2 calculating to 5 and now the turn is 4 and the value is 24</div><div class="line">Task 2 calculating to 5 and now the turn is 5 and the value is 120</div><div class="line">Result of task 1: 45</div></pre></td></tr></table></figure>
<h2 id="指定参数为std-launch-deffered"><a href="#指定参数为std-launch-deffered" class="headerlink" title="指定参数为std::launch::deffered"></a>指定参数为std::launch::deffered</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;future&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;thread&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;chrono&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</div><div class="line">    <span class="keyword">auto</span> future1 = async(launch::deferred,[](<span class="keyword">int</span> &amp;&amp;x)-&gt;<span class="keyword">int</span>&#123;</div><div class="line">        <span class="keyword">auto</span> ret = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> i = <span class="number">0</span>; i &lt; x; i++)&#123;</div><div class="line">            this_thread::sleep_for(chrono::seconds(<span class="number">1</span>));</div><div class="line">            ret += i;</div><div class="line">            <span class="built_in">cout</span> &lt;&lt; <span class="string">"Task 1 calculating to "</span>  &lt;&lt; x - <span class="number">1</span> &lt;&lt; <span class="string">" and now the turn is "</span> &lt;&lt; i &lt;&lt; <span class="string">" and the value is "</span> &lt;&lt; ret &lt;&lt; <span class="built_in">endl</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ret;</div><div class="line">    &#125;,<span class="number">10</span>);    <span class="comment">//future1 并不会立即异步执行</span></div><div class="line">    <span class="keyword">auto</span> future2 = async(launch::deferred,[](<span class="keyword">int</span> &amp;&amp;x)-&gt;<span class="keyword">int</span>&#123;</div><div class="line">        <span class="keyword">auto</span> ret = <span class="number">1</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> i = <span class="number">1</span>; i &lt; x + <span class="number">1</span>; i++)&#123;</div><div class="line">            this_thread::sleep_for(chrono::seconds(<span class="number">2</span>));</div><div class="line">            ret *= i;</div><div class="line">            <span class="built_in">cout</span> &lt;&lt; <span class="string">"Task 2 calculating to "</span>  &lt;&lt; x &lt;&lt; <span class="string">" and now the turn is "</span> &lt;&lt; i &lt;&lt; <span class="string">" and the value is "</span> &lt;&lt; ret &lt;&lt; <span class="built_in">endl</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> ret;</div><div class="line">    &#125;,<span class="number">5</span>);    <span class="comment">//future2 并不会立即异步执行</span></div><div class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">"waiting...\n"</span>;</div><div class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Result of task 1: "</span> &lt;&lt; future1.get() &lt;&lt; <span class="built_in">endl</span>; <span class="comment">//future1在此时执行</span></div><div class="line">    <span class="built_in">cout</span> &lt;&lt; <span class="string">"Result of task 2: "</span> &lt;&lt; future2.get() &lt;&lt; <span class="built_in">endl</span>; <span class="comment">//futute2在此时执行</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="输出-1"><a href="#输出-1" class="headerlink" title="输出"></a>输出</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">c:\Users\yixian\workspace\test&gt;async</div><div class="line">waiting...</div><div class="line">Task 1 calculating to 9 and now the turn is 0 and the value is 0</div><div class="line">Task 1 calculating to 9 and now the turn is 1 and the value is 1</div><div class="line">Task 1 calculating to 9 and now the turn is 2 and the value is 3</div><div class="line">Task 1 calculating to 9 and now the turn is 3 and the value is 6</div><div class="line">Task 1 calculating to 9 and now the turn is 4 and the value is 10</div><div class="line">Task 1 calculating to 9 and now the turn is 5 and the value is 15</div><div class="line">Task 1 calculating to 9 and now the turn is 6 and the value is 21</div><div class="line">Task 1 calculating to 9 and now the turn is 7 and the value is 28</div><div class="line">Task 1 calculating to 9 and now the turn is 8 and the value is 36</div><div class="line">Task 1 calculating to 9 and now the turn is 9 and the value is 45</div><div class="line">Result of task 1: 45</div><div class="line">Task 2 calculating to 5 and now the turn is 1 and the value is 1</div><div class="line">Task 2 calculating to 5 and now the turn is 2 and the value is 2</div><div class="line">Task 2 calculating to 5 and now the turn is 3 and the value is 6</div><div class="line">Task 2 calculating to 5 and now the turn is 4 and the value is 24</div><div class="line">Task 2 calculating to 5 and now the turn is 5 and the value is 120</div><div class="line">Result of task 2: 120</div></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2017/03/31/Modern-C-现代C-中优雅的异步操作/" data-id="cj0xx7zq40003rculgl219ued" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/03/31/【Python运维】最简单的Python运维脚本/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本
        
      </div>
    </a>
  
  
    <a href="/2017/03/31/动态修改Python对象/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">动态修改Python对象</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">March 2017</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2017/03/31/【Python运维】最简单的Python运维脚本/">【Python运维】最简单的Python运维脚本Python运维】最简单的Python运维脚本</a>
          </li>
        
          <li>
            <a href="/2017/03/31/Modern-C-现代C-中优雅的异步操作/">[Modern C++]现代C++中优雅的异步操作</a>
          </li>
        
          <li>
            <a href="/2017/03/31/动态修改Python对象/">动态修改Python对象</a>
          </li>
        
          <li>
            <a href="/2017/03/31/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>

  </div>
</body>
</html>